// pages/setting/setting.js
import {
  MyModel
} from '../../models/my'
const myModel = new MyModel()
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsName: '点击以下油号选择',
    currentIndex: null,
    goods: [],
    gasoLine: [], //汽油
    dieselOil: [] //柴油
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const shopId = app.globalData.station.alipay_store_id
    this.getOilPrice(shopId)
    this.loadData()
  },
  loadData() {
    const getOilType = myModel.getOilType().then(res => {
      console.log('promise-111111111111111- ：拿到油号')
      console.log(res)
      this.setData({
        goods: res
      })
    })
    const settingMsgload = myModel.settingMsgload().then(res => {
      console.log('promise-2222222222222222222- ：拿到设置的信息')
      console.log(res)
      if (res.goodsName) {
        this.setData({
          goodsName: res.goodsName
        })
      }
    })
    Promise.all([getOilType, settingMsgload]).then(data => {
      console.log('promise-都加在好了')
      console.log(data)
      let index = this.getActiveIndex()
      console.log('活跃的索引值：' + index)
      if (index || index == 0) {
        this.setData({
          currentIndex: index
        })
      }
    })
  },
  getActiveIndex() {
    const goods = this.data.goods
    const goodsName = this.data.goodsName
    let index = null
    for (let i = 0; i < goods.length; i++) {
      if (goods[i] == goodsName) {
        index = i
      }
    }
    console.log(index)
    return index
  },
  getOilPrice(shopId) {
    myModel.getOilPrice(shopId).then(res => {
      console.log('当前所有的油价---------------')
      console.log(res)
      this.setData({
        gasoLine: res.gasoLine, //汽油
        dieselOil: res.dieselOil //柴油
      })
    })
  },
  onGoods(e) {
    const index = e.currentTarget.dataset.index
    const goodsName = e.currentTarget.dataset.goodsName
    const userId = app.globalData.user.id
    myModel.settingMsgSave(userId, goodsName, '').then(res => {
      this.setData({
        currentIndex: index,
        goodsName
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})